package com.sfeir.poc.portal.gui.client.widgets;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.sfeir.poc.portal.gui.client.objects.News;

/**
 * Very simple slideshow of pictures
 * Have to be improoved in order to get a better visual aspect, more functionalities and
 * maybe build something based on picasaweb, flickr, ... using API
 * @author bostoen.v
 *
 */
public class SlideshowWidget extends AbstractWidget {

	Grid globalContainer = null;
	Boolean isActive = null;
	private int i = 0;
	Timer timer = null;
	List<News> images = null;
	
	/**
	 * Initialize a timer that call the displayPicture method
	 * @param images
	 */
	public SlideshowWidget(final List<News> images){
		super();
		this.images = images;
		
		globalContainer = new Grid();
		globalContainer.setSize("100%", "300px");
		globalContainer.resize(2, 1);
		globalContainer.getCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		globalContainer.getCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		globalContainer.getCellFormatter().setHeight(0, 0, "275px");
		
		widgetContainer.add(globalContainer);

		final Grid controlGrid = new Grid();
		controlGrid.resize(1, 3);
		
		//Add buttons to control Grid
		Map<Integer, Button> buttons = getButtons();
		for (Iterator<Integer> iterator = buttons.keySet().iterator(); iterator.hasNext();) {
			Integer pos = (Integer) iterator.next();
			controlGrid.setWidget(0, pos, buttons.get(pos));
		}
		
		globalContainer.setWidget(1, 0, controlGrid);
		
		initTimer();
		startSlideshow();
	}

	/**
	 * Initialize the timer that ask the next picture
	 */
	private void initTimer(){
		timer = new Timer() {
			public void run() {
				displayNext();
			}
		};
	}
	
	/**
	 * Get the current picture according to the current index : i
	 * @return
	 */
	private Image getCurrentPicture(){
		if (i >= images.size()) {
			i = 0;
		}else if(i<0){
			i = images.size() -1;
		}
		return new Image(images.get(i).getUrl());
	}
	
	/**
	 * Display the current picture into the widget
	 * @param image
	 */
	private void displayCurrentPicture() {
		globalContainer.setWidget(0, 0, getCurrentPicture());
	}
	
	/**
	 * Activate the slideshow
	 */
	private void startSlideshow(){
		isActive = true;
		timer.schedule(1000);
		timer.scheduleRepeating(4000);
	}
	
	/**
	 * Stop the slideshow
	 */
	private void stopSlideshow(){
		isActive = false;
		timer.cancel();
	}
	
	/**
	 * Display the previous picture according to the current index
	 */
	private void displayPrevious() {
		i--;
		displayCurrentPicture();
	}
	
	/**
	 * Display the next picture according to the current index
	 */
	private void displayNext() {
		i++;
		displayCurrentPicture();
	}
	
	/**
	 * Get the buttons to add to the widget
	 * @return
	 */
	private Map<Integer, Button> getButtons(){
		Map<Integer, Button> buttons = new HashMap<Integer, Button>();

		Button toLeftBt = new Button("&lt;");
		buttons.put(0, toLeftBt);
		Button toRightBt = new Button("&gt;");
		buttons.put(2, toRightBt);
		final Button playBt = new Button("||");
		buttons.put(1, playBt);
		
		toRightBt.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				displayNext();
			}
		});
		
		toLeftBt.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				displayPrevious();
			}
		});
		
		playBt.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				if(isActive){
					stopSlideshow();
					playBt.setText("Go");
					
				}else{
					startSlideshow();
					playBt.setText("||");
				}
			}
		});
		return buttons;
	}
}
